package com.zyp.test.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author zyp
 *         2018/1/9
 */

public class Main10Activity extends AppCompatActivity {

    private TextView mTv_out;
    private EditText mEt_input;
    private Button mBt_send;
    private Socket mSocket;
    private OutputStream mOutputStream;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main10);
        mTv_out = (TextView) findViewById(R.id.tv_out);
        mEt_input = (EditText) findViewById(R.id.et_input);
        mBt_send = (Button) findViewById(R.id.bt_send);
        mBt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMsg();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                createSocket();
                try {
                    InputStream inputStream = mSocket.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    while (true){
                        final String s = bufferedReader.readLine();
                        if("exit".equals(s)){
                            bufferedReader.close();
                            inputStream.close();
                            break;
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTv_out.setText(s);
                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private void sendMsg(){
       new Thread(new Runnable() {
           @Override
           public void run() {
               try {
                   createSocket();
                   mOutputStream = mSocket.getOutputStream();
                   String s = mEt_input.getText().toString();
                   if(!s.endsWith("\n")){
                       s+="\n";
                   }
                   mOutputStream.write(s.getBytes());
               } catch (IOException e) {
                   e.printStackTrace();
               } 
           }
       }).start();
    }

    private void createSocket() {
        if(mSocket == null){
            try {
                mSocket = new Socket("192.168.31.215",8888);
            } catch (final IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTv_out.setText(e.getMessage());
                    }
                });
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mOutputStream != null){
            try {
                mOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
